package cn.lyq.card.ui.address

import cn.lyq.card.base.SimpleVM
import cn.lyq.card.data.model.AddressInfo


class AddressVM : SimpleVM() {

    var delIndex = -1
    fun del(id: Int) = simpleEmit {
        repository.userService.delAddress(id.toString())
    }

    fun getList() = simpleEmit {
        repository.userService.getAddressList()
    }

    fun editAndAdd(address: AddressInfo) = simpleEmit {
        val id = if (address.id == -1) "" else address.id.toString()
        repository.userService.editAddress(
            address.trueName ?: "",
            address.mobile ?: "",
            address.address ?: "",
            address.detailsAddress ?: "",
            address.default,
            id
        )
    }

    fun getRegions(parentId: String) = simpleEmit {
        repository.userService.getRegions(parentId)
    }
}